package com.news.dao;

import com.news.dao.UserDao;
import com.news.entity.News;
import com.news.entity.User;
import com.news.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsDaoImpl implements NewsDao {
    @Override
    public int insertNews(News news) {
        Connection conn = DBUtil.getConnection();
        PreparedStatement stmt = null;
        String sql = "insert into sys_news(NAME,DEPARTMENT,NEWSTIME,NEWS) values(?,?,now(),?)";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, news.getName());
            stmt.setString(2, news.getDepartment());
            stmt.setString(3, news.getNews());
            return stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, stmt, null);
        }
        return 0;
    }

    @Override
    public int updateNews(News news) {
        Connection conn = DBUtil.getConnection();
        PreparedStatement stmt = null;
        String sql = "update sys_user set NAME=?,DEPARTMENT=?,UPTIME=now(),NEWS=? where ID=?";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, news.getName());
            stmt.setString(2, news.getDepartment());
            stmt.setString(3, news.getNews());
            stmt.setInt(4, news.getId());
            return stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, stmt, null);
        }
        return 0;
    }

    @Override
    public int deleteNews(int id) {
        Connection conn = DBUtil.getConnection();
        PreparedStatement stmt = null;
        String sql = "delete from sys_news where ID=?";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, id);
            return stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, stmt, null);
        }
        return 0;
    }

    @Override
    public int deleteNewsBatch(String[] ids) {
        return 0;
    }

    @Override
    public int deleteNewsBatch2(String[] ids) {
        return 0;
    }

    @Override
    public News selectOneNews(int id) {
        Connection conn = DBUtil.getConnection();
        PreparedStatement stmt = null;
        ResultSet rs = null;
        String sql = "select ID,NAME,DEPARTMENT,NEWSTIME,NEWS from sys_news where ID=?";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, id);
            rs = stmt.executeQuery();
            if(rs.next()){
                News news = new News();
                news.setId(rs.getInt("ID"));
                news.setName(rs.getString("NAME"));
                news.setDepartment(rs.getString("DEPARTMENT"));
                news.setNewstime(rs.getTimestamp("NEWSTIME"));
                news.setNews(rs.getString("NEWS"));
                return news;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, stmt, rs);
        }
        return null;
    }

    @Override
    public List<News> selectNews(int offse, int pageSiz, String name) {
        Connection conn = DBUtil.getConnection();
        PreparedStatement stmt = null;
        ResultSet rs = null;
        String sql1 = "select ID,NAME,DEPARTMENT,NEWSTIME,NEWS from sys_news order by ID limit ?,?";
        String sql2 = "select ID,NAME,DEPARTMENT,NEWSTIME,NEWS from sys_news where NAME like concat('%',?,'%') order by ID limit ?,?";
        List<News> list = new ArrayList<News>();
        try {
            if(name==null || name.equals("")){
                stmt = conn.prepareStatement(sql1);
                stmt.setInt(1, offse);
                stmt.setInt(2, pageSiz);
            }else{
                stmt = conn.prepareStatement(sql2);
                stmt.setString(1, name);
                stmt.setInt(2, offse);
                stmt.setInt(3, pageSiz);
            }

            rs = stmt.executeQuery();
            while(rs.next()){
                News news = new News();
                news.setId(rs.getInt("ID"));
                news.setName(rs.getString("NAME"));
                news.setDepartment(rs.getString("Department"));
                news.setNews(rs.getString("News"));
                news.setNewstime(rs.getTimestamp("Newstime"));
                list.add(news);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, stmt, rs);
        }
        return list;
    }

    @Override
    public int selectCount(String acct) {
        return 0;
    }
}
